package com.xidian.zhiancloud.service.ledgerManager.relatedPart;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xidian.zhiancloud.dao.RelatedPartyBlacklistMapper;
import com.xidian.zhiancloud.dao.RelatedPartyDocMapper;
import com.xidian.zhiancloud.dao.syset.SysUserMapper;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.exception.CustomizeException;
import com.xidian.zhiancloud.model.syset.SysUser;
import com.xidian.zhiancloud.qo.ledgerManagerment.relatedParty.RelatedPartyBlacklistQO;
import com.xidian.zhiancloud.vo.ledgerManagerment.relatedParty.RelatedPartyBlackListVO;
import com.xidian.zhiancloud.model.RelatedPartyBlacklist;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: Summerless
 * @Date: 2020/11/8 23:25
 */
@Service
public class RelatedPartyBlacklistService extends ServiceImpl<RelatedPartyBlacklistMapper, RelatedPartyBlacklist> implements IService<RelatedPartyBlacklist> {
    @Autowired
    private RelatedPartyBlacklistMapper relatedPartyBlacklistMapper;

    @Autowired
    private RelatedPartyDocMapper relatedPartyDocMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    public IPage<RelatedPartyBlackListVO> getListByQO(RelatedPartyBlacklistQO relatedPartyBlacklistQO) {
        Page<RelatedPartyBlackListVO> page = new Page<>(relatedPartyBlacklistQO.getCurrent(), relatedPartyBlacklistQO.getSize());
        OrderItem orderItem = new OrderItem();
        orderItem.setColumn("create_time").setAsc(false);
        page.getOrders().add(orderItem);
        return relatedPartyBlacklistMapper.getListByQO(page,relatedPartyBlacklistQO);
    }

    public RelatedPartyBlackListVO getVOById(Long id) {
        RelatedPartyBlacklist relatedPartyBlacklist = relatedPartyBlacklistMapper.selectById(id);
        if (relatedPartyBlacklist == null) {
            throw new CustomizeException(GeneralResponseEnums.GET_FAILED);
        }
        RelatedPartyBlackListVO vo = new RelatedPartyBlackListVO();
        BeanUtils.copyProperties(relatedPartyBlacklist, vo);
        SysUser user = sysUserMapper.selectById(vo.getCreatorId());
        vo.setCreatorName(user == null ? "该用户已被删除" : user.getName());
        return vo;
    }
}
